package com.didispace.service;

import java.util.List;

import rx.Observable;
import rx.Subscriber;import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;

public class TestRxJavaObserve {

	public static void main(String[] args) {
		
		Observable<String> getObservable = getExecutionObservable();
		getObservable.subscribe(new Action1<String>() {

			@Override
			public void call(String t) {
				System.out.println(t);
				
			}
		});
//		getObservable.subscribe();
		Observable<String> myObservable = Observable.create(  
			    new Observable.OnSubscribe<String>() {  
			        @Override  
			        public void call(Subscriber<? super String> sub) {  
			            sub.onNext("Hello, eshin!");  
				            sub.onCompleted();  
				        }
				    }  
				);  
		Subscriber<String> mySubscriber = new Subscriber<String>() {  
			@Override  
			public void onNext(String s) { System.out.println(s); }  
			
			@Override  
			public void onCompleted() { }  
			
			@Override  
			public void onError(Throwable e) { }  
		}; 
		
		myObservable.subscribe(mySubscriber);  
/////////////////////////////////////////////////////////////////////////////////////////////		
		
		Observable.just("hello eshin")
		.map(new Func1<String, String>() {

			@Override
			public String call(String t) {
				// TODO Auto-generated method stub
				return "call:" + t;
			}
		})
		.map(new Func1<String, Integer>() {

			@Override
			public Integer call(String t) {
				// TODO Auto-generated method stub
				System.out.println(t);
				return t.length();
			}
		})
		.subscribe(new Action1<Integer>() {

			@Override
			public void call(Integer t) {
				System.out.println(t);
			}
		});
/////////////////////////////////////////////////////////////////////////////////////////////		
	
		Observable.from(new String[]{"hello","hi"}).subscribe(new Action1<String>() {

			@Override
			public void call(String t) {
				System.out.println(t);
			}
		});
		
		Observable<Integer> obs = Observable.just("hello eshin").flatMap(new Func1<String, Observable<Integer>>() {

			@Override
			public Observable<Integer> call(String t) {
				// TODO Auto-generated method stub
				return Observable.just(t.length());
			}
		});

		obs.subscribe(new Subscriber<Integer>() {

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				System.out.println("finish");
			}

			@Override
			public void onError(Throwable e) {
				// TODO Auto-generated method stub
				System.out.println(e.getMessage());
			}

			@Override
			public void onNext(Integer t) {
				// TODO Auto-generated method stub
				System.out.println("length:"+t);
			}

		});
	
		
		obs.concatMap(new Func1<Integer, Observable<String>>() {

			@Override
			public Observable<String> call(Integer t) {
				// TODO Auto-generated method stub
				String length = "length is :" +t;
				return Observable.just(length);
			}
		}).subscribe(new Action1<String>() {

			@Override
			public void call(String t) {
				// TODO Auto-generated method stub
				System.out.println(t);
			}
		});
	
	}
	
	public static Observable<String> getExecutionObservable() {
        return Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                try {
                    return Observable.just("hello game");
                } catch (Throwable ex) {
                    return Observable.error(ex);
                }
            }
        }).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                // Save thread on which we get subscribed so that we can interrupt it later if needed
               System.out.println("game over");
            }
        });
    }
	
}
